﻿/*
 *  XTL - XNA Tool Library, a collection with usefull XNA classes
 * 
 *  Copyright (C) 2010 Sebastian Schäfer
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;

namespace XTL.Input
{
    /// <summary>
    /// the XNAStateMouse is derived from the generic state manager and offers general mouse-based routines
    /// </summary>
    public class XNAStateMouse : XStateManager<MouseState>
    {
        private int deltaX;
        private int deltaY;
        private int deltaScroll;

        /// <summary>
        /// returns: stateOld.X - stateCurrent.X; --> the mouse movement along the X-axis
        /// </summary>
        public int DeltaX { get { return deltaX; } }
        /// <summary>
        /// returns: stateOld.Y - stateCurrent.Y; --> the mouse movement along the Y-axis
        /// </summary>
        public int DeltaY { get { return deltaY; } }
        /// <summary>
        /// returns: stateOld.ScrollWheelValue - stateCurrent.ScrollWheelValue --> the scrollwheel movement
        /// </summary>
        public int DeltaScrollWheel { get { return deltaScroll; } }

        /// <summary>
        /// Returns true if left mouse button is pressed.
        /// </summary>
        public bool IsBtnLeftDown { get { return stateCurrent.LeftButton == ButtonState.Pressed; } }
        /// <summary>
        /// Returns true if middle mouse button is pressed.
        /// </summary>
        public bool IsBtnMiddleDown { get { return stateCurrent.MiddleButton == ButtonState.Pressed; } }
        /// <summary>
        /// Returns true if right mouse button is pressed.
        /// </summary>
        public bool IsBtnRightDown { get { return stateCurrent.RightButton == ButtonState.Pressed; } }

        /// <summary>
        /// Returns true if left mouse button is up.
        /// </summary>
        public bool BtnUpLeft { get { return !IsBtnLeftDown && (stateOld.LeftButton == ButtonState.Pressed); } }
        /// <summary>
        /// Returns true if middle mouse button is up.
        /// </summary>
        public bool BtnUpMiddle { get { return !IsBtnMiddleDown && (stateOld.MiddleButton == ButtonState.Pressed); } }
        /// <summary>
        /// Returns true if right mouse button is up.
        /// </summary>
        public bool BtnUpRight { get { return !IsBtnRightDown && (stateOld.RightButton == ButtonState.Pressed); } }
        /// <summary>
        /// Returns true if left mouse button is down.
        /// </summary>
        public bool BtnDownLeft { get { return IsBtnLeftDown && (stateOld.LeftButton == ButtonState.Released); } }
        /// <summary>
        /// Returns true if middle mouse button is down.
        /// </summary>
        public bool BtnDownMiddle { get { return IsBtnMiddleDown && (stateOld.MiddleButton == ButtonState.Released); } }
        /// <summary>
        /// Returns true if right mouse button is down.
        /// </summary>
        public bool BtnDownRight { get { return IsBtnRightDown && (stateOld.RightButton == ButtonState.Released); } }

        /// <summary>
        /// constructor. Creates now mouse state
        /// </summary>
        /// <param name="state">A mouse state</param>
        public XNAStateMouse(MouseState state)
            : base(state)
        {
            deltaX = 0;
            deltaY = 0;
            deltaScroll = 0;
        }

        /// <summary>
        /// sets the new state object and saves the mouse position.
        /// </summary>
        /// <param name="state">A mouse state</param>
        protected override void SetNewState(MouseState state)
        {
            base.SetNewState(state);

            deltaX = stateOld.X - stateCurrent.X;
            deltaY = stateOld.Y - stateCurrent.Y;
            deltaScroll = stateOld.ScrollWheelValue - stateCurrent.ScrollWheelValue;
        }
    }
}
